
#include <iostream>
#include <string>
#include"search.hpp"

#include "cpp-httplib/httplib.h"

const std::string sorce="data/raw_html/raw.txt";
const std::string base_path="./wwwroot";

const int port=15535;

int main()
{

    ns_search::Searcher se;
    se.InitSearcher(sorce);

    httplib::Server svr;

    svr.set_base_dir(base_path.c_str());

    svr.Get("/s", [& se](const httplib::Request &req, httplib::Response &rsp)
            { 
                //判断url中是否含有该部分的关键字
                if(!req.has_param("word"))
                {
                    rsp.set_content("请输入搜索关键字......\n","text/plain;charset=utf-8");
                    return;
                }
                std::string key_word=req.get_param_value("word");

                if(key_word.size()==0)
                {
                    rsp.set_content("搜索关键字为空....\n","text/plain;charset=utf-8");
                    return;
                }

                std::cout<<"用户正在搜索："<<key_word<<std::endl;

                std::string json_string;

                se.Serach(key_word,&json_string);

                rsp.set_content(json_string, "text/plain;charset=utf-8");

             });

    std::cout<<"正在监听"<<port<<"端口......"<<std::endl;

    
    svr.listen("0.0.0.0", port);
    return 0;
}


